package com.jframe.sso.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.jframe.sso.model.CurUser;
import com.jframe.sso.model.Role;
import com.jframe.sso.service.RoleService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.BaseAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.controller.utils.SessionContext;
import com.jframe.sys.controller.utils.TabmCache;
import com.jframe.sys.orm.utils.ResultSet;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;


public class OnlineUserAction extends ExtendAction{
	
	public void list(){
		//加载数据字典
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();
		frozenColumns.add(new Columns("ck","",40,""));
		frozenColumns.add(new Columns("sessionId", "sessionId", 250,""));
		frozenColumns.add(new Columns("userNo", "账号", 100,""));
		commonColumns.add(new Columns("userName","用户名",120,""));
		commonColumns.add(new Columns("roleId","角色",120,new Editor("combotree",true),TabmCache.getCombotreeJson("sm_sys_role",true)));
		//commonColumns.add(new Columns("memberId","成员",120,""));
		commonColumns.add(new Columns("ip","登陆ip",180,""));
		commonColumns.add(new Columns("loginTime","登陆时间",150,""));
		//commonColumns.add(new Columns("deptId","部门",120,""));
		Grid grid = new Grid();
		grid.setIdField("sessionId");
		grid.setSortName("loginTime");
		grid.setSortOrder("desc");
		grid.setUrl("onlineUserAction.do?method=load");
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/jframe/sso/list/onlineuserList.jsp");
	}
	
	public void load(){
		int start = this.pageInfo.getGridStart(request);
		int limit = this.pageInfo.getGridLimit(request);
		String userNo = this.request.getParameter("userNo");
		String userName = this.request.getParameter("userName");
		ResultSet<CurUser> rs = new ResultSet<CurUser>();
		List<CurUser> ls = new ArrayList<CurUser>();
		Set<String> set = (Set<String>)this.session.getServletContext().getAttribute("registerUser");
		for(String sessionId:set){
			HttpSession session = SessionContext.getSession(sessionId);
			CurUser curUser = (CurUser)session.getAttribute("curUser");
			if(StringUtil.null2Space(userNo).equals("")&&StringUtil.null2Space(userName).equals("")){
				ls.add(curUser);
			}else if(StringUtil.null2Space(userNo).equals("")&&!StringUtil.null2Space(userName).equals("")){
				if(curUser.getUserName().indexOf(userName)!=-1){
					ls.add(curUser);
				}
			}else if(!StringUtil.null2Space(userNo).equals("")&&StringUtil.null2Space(userName).equals("")){
				if(curUser.getUserNo().indexOf(userNo)!=-1){
					ls.add(curUser);
				}
			}else{
				if(curUser.getUserNo().indexOf(userNo)!=-1&&curUser.getUserName().indexOf(userName)!=-1){
					ls.add(curUser);
				}
			}
		}
		
		int len=limit;
		if(ls.size()<limit){
			len=ls.size();
		}
		List<CurUser> list = new ArrayList<CurUser>();
		for(int i=start;i<start+len;i++){
			list.add(ls.get(i));
		}
		rs.setRows(list);
		rs.setTotal(ls.size());
		this.print(JsonUtil.obj2Json(rs));
	}
	
	public void removeSession(){
		String sessionId = this.request.getParameter("id");		
		Set<String> set = (Set<String>)this.session.getServletContext().getAttribute("registerUser");		
		if(set.contains(sessionId)){
			HttpSession session = SessionContext.getSession(sessionId);
			session.invalidate();
			this.print("true");
		}else{
			this.print("false");
		}		
	}
	
	public void getRoles(){
		RoleService roleService = (RoleService)this.getBean("roleService");
		List<Role> rolels =  roleService.find("from Role where active=? order by id",1);
		String json = "[";
		for(Role role:rolels){
			json +="{'id':"+role.getId()+",";
			json +="'name':'"+role.getName()+"'},";		
		}
		//去除尾部逗号		
		if(rolels.size()>0){
			json = json.substring(0,json.length()-1);
		}	
		json += "]";
		this.print(json);
	}
}
